package com.rollcall;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collectors;

public class RollCallApp {
        public static void main(String[] args) {
        // 记录开始时间
        long startTime = System.currentTimeMillis();
        try {
            // 1. 初始化语音合成
            TTSUtil.initialize();

            // 2. 从数据库获取学生列表
            List<Student> students = DatabaseUtil.getStudentsInOrder();

            if (students.isEmpty()) {
                System.out.println("没有找到学生数据！");
                return;
            }

            // 3. 获取考勤统计数据
            int totalStudents = students.size();
            List<Student> absentStudents = students.stream()
                    .filter(s -> s.getAbsence() == 1)
                    .collect(Collectors.toList());
            int absentCount = absentStudents.size();
            int presentCount = totalStudents - absentCount;

            // 4. 打印考勤表头
            printAttendanceHeader(totalStudents, presentCount, absentCount);

            // 5. 开始顺序点名
            System.out.println("=== 班级点名系统 ===");
            System.out.println("找到" + students.size() + "名学生");
            System.out.println("按回车键开始点名...");

            new Scanner(System.in).nextLine();

            TTSUtil.speak("点下名");
            for (Student student : students) {
                if (student.getAbsence() == 1) {
                    System.out.println(student.getName() + "（请假）");
                    continue; // 跳过请假的学生
                }

                System.out.println("正在点名: " + student.getName());
                // 使用优化后的发音校正
                String nameToSpeak = PronunciationUtil.handleNamePronunciation(student.getName());
                TTSUtil.speak(nameToSpeak);

                
            }

            System.out.println("点名完成！");

            // 6. 打印考勤汇总
            printAttendanceSummary(totalStudents, presentCount, absentStudents);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            TTSUtil.shutdown();
        }

        // 记录结束时间
        long endTime = System.currentTimeMillis();
        printExecutionTime(startTime, endTime);
        
    }

    /**
     * 打印考勤表头信息
     */
    private static void printAttendanceHeader(int total, int present, int absent) {
        // 获取当天日期
        LocalDate today = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年M月d日");
        String dateStr = today.format(formatter);

//        System.out.println("\n=== 班级点名系统 ===");
        System.out.println(dateStr);
        System.out.println(" 班级人数" + total + "人");
        System.out.println(" 应该出勤" + present + "人");
    }

    /**
     * 打印考勤汇总信息
     */
    private static void printAttendanceSummary(int total, int present, List<Student> absentStudents) {
        System.out.println("\n=== 点名完成 ===");
        System.out.println("【考勤汇总】");
        System.out.println("应到人数: " + total + "人");
        System.out.println("实到人数: " + present + "人");
        System.out.println("请假人数: " + absentStudents.size() + "人");

        if (!absentStudents.isEmpty()) {
            System.out.println("\n【请假名单】");
            absentStudents.forEach(s -> System.out.println(s.getName()));
        }

        System.out.println("\n=== 考勤结束 ===");
    }

    /**
     * 打印程序执行时间
     */
    private static void printExecutionTime(long startTime, long endTime) {
        long duration = endTime - startTime;
        long minutes = duration / 60000;
        long seconds = (duration % 60000) / 1000;
        System.out.println("\n程序执行耗时: " + minutes + " 分钟 " + seconds + " 秒");
    }
}
